from sympy.unify.core import partition


def quicksort_iterative(arr):
    stack = [(0, len(arr) - 1)]
    while stack:
        low, high = stack.pop()
        if low >= high:
            continue
        pivot_idx = partition(arr, low, high)  # 与递归版相同的单次分区操作
        stack.append((low, pivot_idx - 1))  # 先压入较小区间保证栈平衡
        stack.append((pivot_idx + 1, high))  # 再处理较大区间
    return arr
